{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c1b6e617-7832-4d9d-8aa4-73060b662d54",
   "metadata": {},
   "source": [
    "# LoupeR demo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1acfba16-fbd3-4429-b73f-56cb0368ed28",
   "metadata": {},
   "source": [
    "October 16, 2023"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09357ae0-9cb8-4a7e-9d19-a59048a927b4",
   "metadata": {},
   "source": [
    "LoupeR to generate CLOUPE files from Seurat objects:\n",
    "https://www.10xgenomics.com/support/software/loupe-browser/tutorials/lb-louper "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e85ddab-23ba-4f17-9942-28639f688505",
   "metadata": {},
   "source": [
    "Available for download from GitHub: https://github.com/10xGenomics/loupeR/"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "261b958e-0d7a-4b83-906e-70207dacf65c",
   "metadata": {},
   "source": [
    "# Install"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b5da3ca-f9ca-4a35-869f-4ea7c4f3c83a",
   "metadata": {},
   "source": [
    "This can take approximately 25 min. Install and load necessary packages for Seurat:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "621bc814-0e4b-4295-8a52-255d027d1fe7",
   "metadata": {},
   "outputs": [],
   "source": [
    "install.packages(\"remotes\")\n",
    "if (!require(\"hdf5r\")) install.packages(\"hdf5r\")\n",
    "if (!require(\"Seurat\")) install.packages(\"Seurat\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a26c24f-27a5-4498-89f1-9431037f9938",
   "metadata": {},
   "source": [
    "Install LoupeR:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "728ec65a-6768-4f81-b978-34591926c72c",
   "metadata": {},
   "outputs": [],
   "source": [
    "remotes::install_github(\"10XGenomics/loupeR\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26e44394-fc0c-4010-a3e5-0dd36d97d82f",
   "metadata": {},
   "source": [
    "This section will pause and ask you to accept the End User License Agreement (EULA), type 'y' to continue:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ed209d9-b27f-45f8-9b5b-31d4171276c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "loupeR::setup()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61354571-b114-4514-b439-ed8a6fa1290b",
   "metadata": {},
   "source": [
    "# Download data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6874de1c-2811-4430-acd4-3a42b26c2f2f",
   "metadata": {},
   "source": [
    "Download and extract matrix data to test from this dataset: https://www.10xgenomics.com/resources/datasets/5k-adult-mouse-brain-nuclei-isolated-with-chromium-nuclei-isolation-kit-3-1-standard"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bad627a9-3320-4a88-86f8-a1c7d4fa1dc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "download.file(\"https://cf.10xgenomics.com/samples/cell-exp/7.0.0/5k_mouse_brain_CNIK_3pv3/5k_mouse_brain_CNIK_3pv3_filtered_feature_bc_matrix.tar.gz\",\"filtered_feature_bc_matrix.tar.gz\")\n",
    "untar(\"filtered_feature_bc_matrix.tar.gz\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "464e99da-1b98-4925-8a33-54019fc6b6f3",
   "metadata": {},
   "source": [
    "# Load R libraries"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a96c9656-1679-4aa2-90d0-c4038a357f97",
   "metadata": {},
   "source": [
    "Load Seurat and necessary packages:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd65bc6d-1970-45f2-a34a-78bc5926cbb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "library(Seurat)\n",
    "library(patchwork)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a1e79dc-9fff-442c-be7a-a8464f730a35",
   "metadata": {},
   "source": [
    "Load LoupeR:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bbf856d-718a-486b-a5dd-09d4b83778bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "library(loupeR)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae0cffa6-3789-4c75-8b91-2e4d4ccaf457",
   "metadata": {},
   "source": [
    "# Read"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5184979b-eb44-40c6-94e2-1408b81dd1b8",
   "metadata": {},
   "source": [
    "Read matrix data into Seurat and create a Seraut object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "549fe6a5-d29f-4522-bc19-1de397399fce",
   "metadata": {},
   "outputs": [],
   "source": [
    "adult_mouse_brain_nuclei <- Read10X(data.dir = \"filtered_feature_bc_matrix/\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35fe4755-2ccb-4a46-b17b-e90db3a40083",
   "metadata": {},
   "outputs": [],
   "source": [
    "seurat_obj <- CreateSeuratObject(counts = adult_mouse_brain_nuclei, project = \"louper test\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59f2febf-cca9-4e17-bec0-64780bb3a826",
   "metadata": {},
   "source": [
    "# Data Demo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "637b3b05-3ce5-4640-84c6-bd502c046a4f",
   "metadata": {},
   "source": [
    "Standard pre-processing workflow (normalization, scaling, clustering, dimensionality reduction)\n",
    "based on Seurat vignette here: https://satijalab.org/seurat/articles/pbmc3k_tutorial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bf20fe2-812f-4b08-925e-d77f303729a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "seurat_obj <- NormalizeData(seurat_obj, normalization.method = \"LogNormalize\", scale.factor = 10000)\n",
    "seurat_obj <- FindVariableFeatures(seurat_obj, selection.method = \"vst\", nfeatures = 2000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "741b4439-e9b8-4b00-8774-e864bf09ac08",
   "metadata": {},
   "outputs": [],
   "source": [
    "all.genes <- rownames(seurat_obj)\n",
    "seurat_obj <- ScaleData(seurat_obj, features = all.genes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "775014d8-87d9-48ef-a9f3-7fbce0b033b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "seurat_obj <- RunPCA(seurat_obj, features = VariableFeatures(object = seurat_obj))\n",
    "seurat_obj <- FindNeighbors(seurat_obj, dims = 1:10)\n",
    "seurat_obj <- FindClusters(seurat_obj, resolution = 0.5)\n",
    "seurat_obj <- RunUMAP(seurat_obj, dims = 1:10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1625b8b-a272-497a-a271-974c23474b28",
   "metadata": {},
   "source": [
    "# Plot UMAP"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc383f5e-d3a6-4824-a31a-685ae6afcbad",
   "metadata": {},
   "source": [
    "Visualization for comparison, R/Seurat vs Loupe as a sanity check:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfda6404-1253-437d-96b0-9a4ad36b353d",
   "metadata": {},
   "outputs": [],
   "source": [
    "DimPlot(seurat_obj, reduction = \"umap\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61871622-5bfb-412c-be5c-c2a330061c87",
   "metadata": {},
   "source": [
    "# Save .cloupe file"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1a89344-6a84-4ea1-90b3-ce3f4fcf82ac",
   "metadata": {},
   "source": [
    "Saves a file in the working directory called \"louper_test.cloupe\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "912b1af6-1149-4b0d-bfdb-374bcc9226bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "create_loupe_from_seurat(seurat_obj, output_name = \"louper_test\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9bf20527-6345-45e8-88d9-f66d28ddbbb7",
   "metadata": {},
   "source": [
    "# Save Seurat object "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7fc6bff-77fc-4177-8232-0e5b6c1801d0",
   "metadata": {},
   "source": [
    "Save Seurat object as RDS file, to save object for later"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e89840c-2ee6-4d56-8224-120420814a02",
   "metadata": {},
   "outputs": [],
   "source": [
    "saveRDS(seurat_obj, file = \"mouse_brain_nuclei.RDS\") "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a97f8fa7-b6ce-44fd-89ec-30f9fa05b188",
   "metadata": {},
   "source": [
    "Load Seurat object as RDS, to load the object back in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e8eed8b-a98e-4230-afa7-a475e591843b",
   "metadata": {},
   "outputs": [],
   "source": [
    "seurat_obj = readRDS(file = \"mouse_brain_nuclei.RDS\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.2.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
